﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceStack.Redis;

namespace A2DFramework.QueueService.Providers
{
    public static class RedisFactory
    {
        private static PooledRedisClientManager mgr = null;
        private static object lockObj = new object();

        /// <summary>
        /// 从线程池中获取一个客户端代理
        /// </summary>
        /// <returns></returns>
        public static RedisClient GetRedisClient()
        {
            RedisClient client = null;

            lock (lockObj)
            {
                if (mgr == null)
                {
                    mgr = new PooledRedisClientManager(
                                    QueueConfiguration.QueueConfiguration.RedisSettings.ReadWriteServerIps.ToArray(),
                                    QueueConfiguration.QueueConfiguration.RedisSettings.ReadOnlyServerIps.ToArray(), 
                                    new RedisClientManagerConfig() 
                                    { 
                                        AutoStart = true,
                                        MaxReadPoolSize = QueueConfiguration.QueueConfiguration.RedisSettings.MaxReadPoolSize,
                                        MaxWritePoolSize = QueueConfiguration.QueueConfiguration.RedisSettings.MaxWritePoolSize
                                    });
                }

                client = mgr.GetClient() as RedisClient;
            }
            return client;
        }
    }
}
